package com.example.its2021606134chh.BusDiyModule;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
//import android.widget.CalendarView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.its2021606134chh.DAModule.DateActivity;
import com.example.its2021606134chh.MainActivity;
import com.example.its2021606134chh.R;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;

public class BusDiyDateActivity extends AppCompatActivity implements    //CalendarView的导入接口
        CalendarView.OnCalendarMultiSelectListener,
        CalendarView.OnCalendarInterceptListener,
        CalendarView.OnYearChangeListener,
        CalendarView.OnMonthChangeListener,
        CalendarView.OnCalendarSelectListener{

    private ImageView imgBusDateBack;
    private CalendarView calendarBusDate;
    private TextView tvBusDate;
    private Button btnBusDateNext;
    private Button btnBusDateClear;
    private String sDate = "";  //选中日期

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bus_diy_date);
        initView();
        Intent intent1 = getIntent();
        String id = intent1.getStringExtra("id");
        String start_place = intent1.getStringExtra("start_place");
        String end_place = intent1.getStringExtra("end_place");
        String price = intent1.getStringExtra("price");
        imgBusDateBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(BusDiyDateActivity.this, MainActivity.class));
                finish();
            }
        });
        calendarBusDate.setOnCalendarMultiSelectListener(this); //日期多选事件
        calendarBusDate.setOnYearChangeListener(this);  //年份改变事件
        calendarBusDate.setOnCalendarSelectListener(this);  //日期选择事件
        btnBusDateClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvBusDate.setText("");
                sDate = "";
                calendarBusDate.clearMultiSelect(); //清除多选
            }
        });
        btnBusDateNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BusDiyDateActivity.this, BusDiyPlaceActivity.class);
                if (sDate.endsWith(",")) {
                    sDate = sDate.substring(0, sDate.length() - 1);
                }
                Log.i("TAG", sDate);
                intent.putExtra("sDate", sDate);
                intent.putExtra("id", id);
                intent.putExtra("start_place", start_place);
                intent.putExtra("end_place", end_place);
                intent.putExtra("price", price);
                startActivity(intent);
                finish();
            }
        });
    }

    private void initView() {
        imgBusDateBack = (ImageView) findViewById(R.id.imgBusDateBack);
        calendarBusDate = (CalendarView) findViewById(R.id.calendarBusDate);
        tvBusDate = (TextView) findViewById(R.id.tvBusDate);
        btnBusDateNext = (Button) findViewById(R.id.btnBusDateNext);
        btnBusDateClear = (Button) findViewById(R.id.btnBusDateClear);
    }

    @Override
    public boolean onCalendarIntercept(Calendar calendar) {
        //Log.e("onCalendarIntercept", calendar.toString());
        int day = calendar.getDay();
//        return day == 1 || day == 3 || day == 6 || day == 11 ||
//                day == 12 || day == 15 || day == 20 || day == 26;
//        return calendar.hasScheme();
        return calendar.getYear()<= calendarBusDate.getCurYear() &&
                calendar.getMonth() <= calendarBusDate.getCurMonth() &&
                calendar.getDay() < calendarBusDate.getCurDay();
    }

    @Override
    public void onCalendarInterceptClick(Calendar calendar, boolean isClick) {
        Toast.makeText(this,
                calendar.toString() + (isClick ? "拦截不可点击" : "拦截设定为无效日期"),
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCalendarMultiSelectOutOfRange(Calendar calendar) {
        Log.e("OutOfRange", "OutOfRange" + calendar);
    }

    @Override
    public void onMultiSelectOutOfSize(Calendar calendar, int maxSize) {
        Toast.makeText(this, "超过最大选择数量 ：" + maxSize, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCalendarMultiSelect(Calendar calendar, int curSize, int maxSize) {
        Log.e("onDateSelected", "  -- " + calendar.getYear() +
                "  --  " + calendar.getMonth() +
                "  -- " + calendar.getDay() +
                "  --  " + "  --   " + calendar.getScheme());

        String content;
        int year = calendar.getYear();
        int month = calendar.getMonth();
        int dayOfMonth = calendar.getDay();
        if (month < 10) {
            if (dayOfMonth < 10) {
                content = year+"-0"+month+"-0"+dayOfMonth;
            } else {
                content = year+"-0"+month+"-"+dayOfMonth;
            }
        } else {
            if (dayOfMonth < 10) {
                content = year+"-"+month+"-0"+dayOfMonth;
            } else {
                content = year+"-"+month+"-"+dayOfMonth;
            }
        }
        Toast.makeText(BusDiyDateActivity.this, "你选择了:\n"+content, Toast.LENGTH_SHORT).show();
        int result = sDate.indexOf(content);
        if (result == -1) {
            sDate += content + ",";
            tvBusDate.setText(sDate);
        }
    }

    @Override
    public void onCalendarOutOfRange(Calendar calendar) {

    }

    @Override
    public void onCalendarSelect(Calendar calendar, boolean isClick) {
        Log.e("onDateSelected", "  -- " + calendar.getYear() +
                "  --  " + calendar.getMonth() +
                "  -- " + calendar.getDay() +
                "  --  " + isClick + "  --   " + calendar.getScheme());
    }

    @Override
    public void onMonthChange(int year, int month) {
        Log.e("onMonthChange", "  -- " + year + "  --  " + month);
    }

    @Override
    public void onYearChange(int year) {
        Log.i("onYearChange", "  -- " + year);
    }
}